package com.study.service.communication.hystrix;

import com.study.service.communication.AuthorityFeignClient;
import com.study.vo.JwtToken;
import com.study.vo.UsernameAndPassword;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * openfeign 集成 hystrix 的另一种模式
 * AuthorityFeignClient 后备 FallbackFactory 、带有异常
 */
@Component
@Slf4j
public class AuthorityFeignClientFallbackFactory
        implements FallbackFactory<AuthorityFeignClient> {
    @Override
    public AuthorityFeignClient create(Throwable throwable) {

        log.warn("authority feign client get token by feign request error" +
                "(hystrix fallbackfactory):[{}]",throwable.getMessage(),throwable);

        return new AuthorityFeignClient() {
            @Override
            public JwtToken getTokenByFeign(UsernameAndPassword usernameAndPassword) {
                return new JwtToken("getTokenByFeign-factory");
            }
        };
    }
}
